/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pv1682010;

import java.util.Collection;
import java.util.LinkedList;

/**
 *
 * @author Isamaru
 */
public class Course implements Comparable<Course> {

    private Integer id;
    private String name;
    private String description;
    private Collection<User> teachers;

    public Course(){
    }

    public Course(Integer id, String name, String description, Collection<User> teachers) {
        setId(id);
        setName(name);
        setDescription(description);
        setTeachers(teachers);
    }

    public Course(String name, String description, Collection<User> teachers) {
        this(null, name, description, teachers);
    }

    public Course(String name, String description) {
        this(name, description, new LinkedList<User>());
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Collection<User> getTeachers() {
        return teachers;
    }

    public void setTeachers(Collection<User> teachers) {
        if (teachers == null) {
            throw new NullPointerException("teachers");
        }
        this.teachers = teachers;
    }

    public void addTeacher(User teacher) {
	if (teacher.getUco() <= 0) {
	    throw new IllegalArgumentException("teacher");
	}
	if (!teachers.contains(teacher)) {
	    teachers.add(teacher);
	}
    }

    @Override
    public String toString() {
        return "Course " + name + " identified by " + id + " with description " + description + " taught by " + teachers;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Course other = (Course) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    public int compareTo(Course other) {
        if (equals(other)) {
            return 0;
        }
        if (getId() == null) { // can't be null, otherwise equal
            return 1;
        }
        if (other.getId() == null) { // can't be null, otherwise equal
            return -1;
        }
        return getId().compareTo(other.getId());
    }
}
